package uk.co.jonlt.AndBus;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import uk.co.jonlt.AndBus.Stop;
import uk.co.jonlt.AndBus.StopAdapter;

public class SearchActivity extends Activity implements OnClickListener
{
	ListView list;
	StopAdapter adapter;
	List<Stop> listOfStop;
	Timer timer;
	
	GetXMLTask tsk = new GetXMLTask();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
        list = (ListView) findViewById(R.id.ListView02);
        list.setClickable(true);
        
        listOfStop = new ArrayList<Stop>();
       
        //listOfStop.add(new Stop("131","WELLINGTON CRESCENT","75101","TOOTING BROADWAY STATION"));
     
        adapter = new StopAdapter(this, listOfStop);

        list.setAdapter(adapter);
        
        Log.i("*** ","Before loading");
        
	}
	
	/** Called when the activity is paused. **/
    @Override
    public void onPause()
    {
    	Log.i("INFO","OnPaused");
    	timer.cancel();
    	super.onPause();
    } 
    
    /** Called when the activity starts **/ 
    @Override
    public void onResume() 
    {
    	Log.i("INFO","OnResume");
    	toCallAsync();
    	super.onResume();
    } 

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	public void toCallAsync()
	{
	    TimerTask doAsync;
	    final Handler handler = new Handler();
	    timer = new Timer();
	    	
	    doAsync = new TimerTask()
	    {
	    	@Override
	    	public void run()
	    	{
	    		handler.post(new Runnable()
	    		{
	    			public void run()
	    			{
	    				try
	    	    		{
	    	    			GetXMLTask tsk = new GetXMLTask();
	    	    			tsk.execute("http://jonl-t.co.uk/tfl/countdown/neareststops/51.4027/-0.2653");
	    	    		}
	    	    		catch (Exception e)
	    	    		{}
	    			}
	    		});
	    		
	    	}
	    };
	    timer.schedule(doAsync,0,2000);
	    			
	}
	
	public class GetXMLTask extends AsyncTask<String, Void, String>
	{
	    @Override
	    protected String doInBackground(String... params) 
	    {
	    	String url = params[0];
	    	Log.i("-_-", "Running task");
	    	String retString = null;
	    	try
	    	{
	    		URL pcode = new URL(url);
	    		HttpURLConnection http = (HttpURLConnection)pcode.openConnection();
	    		InputStream in = new BufferedInputStream(http.getInputStream());
	    			
	    		BufferedReader r = new BufferedReader(new InputStreamReader(in));
	    		StringBuilder total = new StringBuilder();
	    		String line;
	    		while ((line = r.readLine()) != null) {
	    			    total.append(line);
	    		}
	    		retString = total.toString();
	    			
	    		Log.i("+++","Got Data");

	    	}
	    	catch (Exception ex)
	    	{
	    		Log.i("Exception", ex.getMessage());
	    	}
	    	return retString;
	    }
	    
	    @Override
    	protected void onPostExecute(String result)
    	{

    		Stops s = new Stops();
    		try
    		{
    			s.Load(result);
    			List<Stop> stopList = s.stops;
    		
    			listOfStop.clear();
    			listOfStop.addAll(stopList);
    		
    			adapter.notifyDataSetChanged();

    			Log.i("???","Loaded data");
    		}
    			
    		catch (Exception ex)
    		{
    			Log.i("###","Load failed");
    		}
    
    	}
	}
}
